import Vue from 'vue'
import axios from 'axios'
import store from '@/store'
import notification from 'ant-design-vue/es/notification'
import { VueAxios } from './axios'
import { TOKEN_NAME, prodUseMock } from '@/config/index'

let baseURL = prodUseMock ? '/api' : process.env.VUE_APP_API_BASE_URL

// 创建 axios 实例
const service = axios.create({
  baseURL, // api base_url
  withCredentials: true,
  timeout: 6000, // 请求超时时间
  header: {
    cookie: {
      SESSION: 'M2I0NTM5ZWYtMjg5MS00OGQ0LWFmMjMtODIyNmJjMzkxNzc4',
    },
  },
})

// request interceptor
service.interceptors.request.use((config) => {
  const token = Vue.ls.get(TOKEN_NAME)
  if (token) {
    config.headers['Access-Token'] = token // 让每个请求携带自定义 token 请根据实际情况自行修改
  }
  return config
})

// response interceptor
service.interceptors.response.use((response) => {
  return response.data
})

const installer = {
  vm: {},
  install(Vue) {
    Vue.use(VueAxios, service)
  },
}

export { installer as VueAxios, service as axios }
